package com.ramos.kuaixiao.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.ramos.kuaixiao.App;

import java.util.concurrent.atomic.AtomicInteger;


public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "shop.db";
    private static final int DATABASE_VERSION = 3;
    private static DBHelper helper;
    private static SQLiteDatabase db;
    private static AtomicInteger mOpenCounter = new AtomicInteger();

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public synchronized  static DBHelper getInstance() {
        if (helper == null) {
            helper = new DBHelper(App.mContext);
        }
        DBHelper localDbHelper = helper;
        return localDbHelper;
    }

    public synchronized  static SQLiteDatabase open() {
        if(mOpenCounter.incrementAndGet() == 1) {
            if ((db == null) || (!db.isOpen())) {
                db = DBHelper.getInstance().getWritableDatabase();
            }
        }
        return db;
    }

    public synchronized void close() {
        if(mOpenCounter.decrementAndGet() == 0) {
            // Closing database
            db.close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(BrandInfoDB.CREATE_TABLE_SQL);
        db.execSQL(WareHouseDB.CREATE_TABLE_SQL);
        db.execSQL(UnSubmitOrderDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(AllCustomerDB.CREATE_TABLE_SQL);
        db.execSQL(AllCustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(BrandDB.CREATE_TABLE_SQL);
        db.execSQL(ShopsDB.CREATE_TABLE_SQL);
        db.execSQL(StoreNumDB.CREATE_TABLE_SQL);
        db.execSQL(PriceDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerWareHouseDB.CREATE_TABLE_SQL);
        db.execSQL(OrderDB.CREATE_TABLE_SQL);
        db.execSQL(EmpBrandDB.CREATE_TABLE_SQL);
        db.execSQL(PriceTypeDB.CREATE_TABLE_SQL);
        db.execSQL(ClientPriceDB.CREATE_TABLE_SQL);
        db.execSQL(EmpStoreHouseDB.CREATE_TABLE_SQL);
        db.execSQL(ProductTypeDB.CREATE_TABLE_SQL);
        db.execSQL(SaleCustomerDB.CREATE_TABLE_SQL);
        db.execSQL(SaleCustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(BrandKeywordsDB.CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       /* if(oldVersion == 1){
            db.execSQL(StoreNumDB.DROP_USERINFO);
            db.execSQL(StoreNumDB.CREATE_TABLE_SQL);
        }*/
        if(oldVersion == 1 || oldVersion == 2/*|| oldVersion == 3*/){
            App.isOldVersion = true;
            Log.d("onUpgrade", "App.isOldVersion:"+App.isOldVersion);
            db.execSQL(BrandInfoDB.DROP_USERINFO);
            db.execSQL(BrandInfoDB.CREATE_TABLE_SQL);
            db.execSQL(WareHouseDB.DROP_USERINFO);
            db.execSQL(WareHouseDB.CREATE_TABLE_SQL);
            db.execSQL(CustomerDB.DROP_USERINFO);
            db.execSQL(CustomerDB.CREATE_TABLE_SQL);
            db.execSQL(CustomerStoreDB.DROP_USERINFO);
            db.execSQL(CustomerStoreDB.CREATE_TABLE_SQL);
            db.execSQL(AllCustomerDB.DROP_USERINFO);
            db.execSQL(AllCustomerDB.CREATE_TABLE_SQL);
            db.execSQL(AllCustomerStoreDB.DROP_USERINFO);
            db.execSQL(AllCustomerStoreDB.CREATE_TABLE_SQL);
            db.execSQL(BrandDB.DROP_USERINFO);
            db.execSQL(BrandDB.CREATE_TABLE_SQL);
            db.execSQL(ShopsDB.DROP_USERINFO);
            db.execSQL(ShopsDB.CREATE_TABLE_SQL);
            db.execSQL(StoreNumDB.DROP_USERINFO);
            db.execSQL(StoreNumDB.CREATE_TABLE_SQL);
            db.execSQL(PriceDB.DROP_USERINFO);
            db.execSQL(PriceDB.CREATE_TABLE_SQL);
            db.execSQL(CustomerWareHouseDB.DROP_USERINFO);
            db.execSQL(CustomerWareHouseDB.CREATE_TABLE_SQL);
            //db.execSQL(OrderDB.CREATE_TABLE_SQL);
            db.execSQL(EmpBrandDB.DROP_USERINFO);
            db.execSQL(EmpBrandDB.CREATE_TABLE_SQL);
            db.execSQL(PriceTypeDB.DROP_USERINFO);
            db.execSQL(PriceTypeDB.CREATE_TABLE_SQL);
            db.execSQL(ClientPriceDB.DROP_USERINFO);
            db.execSQL(ClientPriceDB.CREATE_TABLE_SQL);
            db.execSQL(EmpStoreHouseDB.DROP_USERINFO);
            db.execSQL(EmpStoreHouseDB.CREATE_TABLE_SQL);
            db.execSQL(ProductTypeDB.DROP_USERINFO);
            db.execSQL(SaleCustomerDB.DROP_USERINFO);
            db.execSQL(SaleCustomerDB.CREATE_TABLE_SQL);
            db.execSQL(SaleCustomerStoreDB.DROP_USERINFO);
            db.execSQL(SaleCustomerStoreDB.CREATE_TABLE_SQL);
            db.execSQL(BrandKeywordsDB.DROP_USERINFO);
            db.execSQL(BrandKeywordsDB.CREATE_TABLE_SQL);

        } else {
            Log.d("onUpgrade", "App.isOldVersion:"+App.isOldVersion);
        }
    }
}